using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Security.Permissions;


namespace Camalon.ColorPicker.Design
{
	/// <summary>
	/// Summary description for TransparenColorEditor.
	/// </summary>
	public class TransparentColorEditor:UITypeEditor
	{
		private static Camalon.ColorPicker.ColorDialog _colorPicker;

		static TransparentColorEditor()
		{
			_colorPicker				= new Camalon.ColorPicker.ColorDialog();
			_colorPicker.FullOpen		= false;
			_colorPicker.FileOpenOnly	= true;
			_colorPicker.AutoName		= true;
			_colorPicker.ViewStyle		= ListViewStyle.Details;
		}

        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
		{
			return true;
		}

        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.Modal;
		}

        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			if( (value is TransparentColor ) || (value is Color ) )
			{
				Color cl ;

				if( value is TransparentColor) cl = ((TransparentColor)value).Color;
				else cl = (Color)value;

				Color[] colors = _colorPicker.GetMyColors(true);

				bool bFound = false;
				foreach( Color c in colors)
				{
					if( c.A== cl.A && c.R== cl.R && c.G == cl.G && c.B == cl.B)
					{bFound = true;	break;	}
				}
			
				if( !bFound) 
				{
					Color[] colorsx = new Color[colors.Length+1];
					colors.CopyTo( colorsx,0);
					colorsx[colors.Length] = cl;
					colors = colorsx;
				}

				if( colors.Length > 0) _colorPicker.SetMyColors( colors, -1);
				_colorPicker.PrimaryColor = cl;

				if( _colorPicker.ShowDialog() == DialogResult.OK) cl = _colorPicker.PrimaryColor;

				if( value is TransparentColor)
				{
					TransparentColor c = (TransparentColor)value;
					c.Color = cl;
					return c;
				}
				else return cl;

			}
		
			return base.EditValue (context, provider, value);
		}



		protected internal static void PaintColorValue( PaintValueEventArgs pe)
		{
			if( pe.Value == null) return;

			bool paintable = ( pe.Value is TransparentColor) || (pe.Value is Color);

			if( !paintable) return;
		
			Color tc;


			if( pe.Value is TransparentColor)	tc = ((TransparentColor)pe.Value).Color;
			else if( pe.Value is Color )		tc = (Color)pe.Value;
			else tc = (Color)pe.Value;

			bool isEmpty = tc == TransparentColor.Empty;


			Rectangle b = pe.Bounds;
			b.Width--;b.Height--;

			Graphics g = pe.Graphics;

			if( isEmpty )
			{
				Point leftTop		= new Point(b.Left, b.Top);
				Point leftBottom	= new Point(b.Left, b.Bottom);
				Point rightTop		= new Point(b.Right, b.Top);
				Point rightBottom	= new Point(b.Right, b.Bottom);
				Pen pen = Pens.Red;
				g.DrawLine( pen,leftTop, rightBottom);
				g.DrawLine( pen,leftBottom, rightTop);
				return;
			}


			if( tc.A < 255)
			{
				RectangleF rt = b;
				rt.Width /=2f;
				rt.Height/=2f;
				g.FillRectangle( Brushes.Black, rt);
				rt.X += rt.Width;
				rt.Y += rt.Height;
				g.FillRectangle( Brushes.Black, rt);
			}

			using ( SolidBrush sb = new SolidBrush( tc)) g.FillRectangle( sb, b);
				

		}

        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override void PaintValue(PaintValueEventArgs e) 
		{
			PaintColorValue( e );

		}
	}

}
